<template>
  <div>
    <h2 class="htit">
      demo示例一
    </h2>
    <div class="box">
      <div
        class="scroller"
        :style="[{height: '375px'}, {overflow: 'hidden'}]"
      >
        <ul
          class="clearfix"
          :style="setStyle"
        >
          <li
            v-for="(item, index) in list1"
            :key="index"
            :class="{cur: listCurrentIndex === index}"
            @click="changeHandle(item, index)"
          >
            <a href="javascript:void(0)">{{ item }}</a>
          </li>
        </ul>
      </div>
    </div>

    <!-- <h2 class="htit">demo示例二</h2>
    <div class="wrapper wrapper02" id="wrapper02">
      <div class="scroller">
        <ul class="clearfix">
          <li>
            <a href="javascript:void(0)">全部科目</a>
          </li>
          <li>
            <a href="javascript:void(0)">会计从业</a>
          </li>
          <li>
            <a href="javascript:void(0)">会计职称</a>
          </li>
          <li>
            <a href="javascript:void(0)">注会</a>
          </li>
          <li>
            <a href="javascript:void(0)">税务师</a>
          </li>
          <li>
            <a href="javascript:void(0)">国际证书</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多注会</a>
          </li>
          <li>
            <a href="javascript:void(0)">国际证书2</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多4</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多5</a>
          </li>
        </ul>
      </div>
    </div>

    <h2 class="htit">demo示例三</h2>
    <div class="wrapper wrapper03" id="wrapper03">
      <div class="scroller">
        <ul class="clearfix">
          <li>
            <a href="javascript:void(0)">全部科目</a>
          </li>
          <li>
            <a href="javascript:void(0)">会计从业</a>
          </li>
          <li>
            <a href="javascript:void(0)">会计职称</a>
          </li>
          <li>
            <a href="javascript:void(0)">注会</a>
          </li>
          <li>
            <a href="javascript:void(0)">税务师</a>
          </li>
          <li>
            <a href="javascript:void(0)">国际证书</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多注会</a>
          </li>
          <li>
            <a href="javascript:void(0)">国际证书2</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多4</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多5</a>
          </li>
        </ul>
      </div>
    </div>

    <h2 class="htit">demo示例四</h2>
    <div class="wrapper wrapper04" id="sass">
      <div class="scroller">
        <ul class="clearfix">
          <li>
            <a href="javascript:void(0)">全部科目</a>
          </li>
          <li>
            <a href="javascript:void(0)">会计从业</a>
          </li>
          <li>
            <a href="javascript:void(0)">会计职称</a>
          </li>
          <li>
            <a href="javascript:void(0)">注会</a>
          </li>
          <li>
            <a href="javascript:void(0)">税务师</a>
          </li>
          <li>
            <a href="javascript:void(0)">国际证书</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多注会</a>
          </li>
          <li>
            <a href="javascript:void(0)">国际证书2</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多4</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多5</a>
          </li>
        </ul>
      </div>
    </div>

    <h2 class="htit">demo示例五</h2>
    <div class="wrapper02" id="demo05">
      <div class="scroller">
        <ul class="clearfix">
          <li>
            <a href="javascript:void(0)">全部科目</a>
          </li>
          <li>
            <a href="javascript:void(0)">会计从业</a>
          </li>
          <li>
            <a href="javascript:void(0)">会计职称</a>
          </li>
          <li>
            <a href="javascript:void(0)">注会</a>
          </li>
          <li>
            <a href="javascript:void(0)">税务师</a>
          </li>
          <li>
            <a href="javascript:void(0)">国际证书</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多注会</a>
          </li>
          <li>
            <a href="javascript:void(0)">国际证书2</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多4</a>
          </li>
          <li>
            <a href="javascript:void(0)">更多5</a>
          </li>
        </ul>
      </div>
    </div>

    <h2 class="htit">demo示例六</h2>
    <div class="wrapper02" id="demo06">
      <div class="scroller">
        <ul class="clearfix">
          <li>
            <a href="javascript:void(0)">北京</a>
          </li>
          <li>
            <a href="javascript:void(0)">上海</a>
          </li>
          <li>
            <a href="javascript:void(0)">广州</a>
          </li>
          <li>
            <a href="javascript:void(0)">深证</a>
          </li>
          <li>
            <a href="javascript:void(0)">杭州</a>
          </li>
          <li>
            <a href="javascript:void(0)">武汉</a>
          </li>
          <li>
            <a href="javascript:void(0)">天津</a>
          </li>
          <li>
            <a href="javascript:void(0)">郑州</a>
          </li>
          <li>
            <a href="javascript:void(0)">海口</a>
          </li>
          <li>
            <a href="javascript:void(0)">郑州</a>
          </li>
          <li>
            <a href="javascript:void(0)">长春</a>
          </li>
          <li>
            <a href="javascript:void(0)">长沙</a>
          </li>
          <li>
            <a href="javascript:void(0)">南京</a>
          </li>
          <li>
            <a href="javascript:void(0)">西安</a>
          </li>
        </ul>
      </div>
    </div> -->
  </div>
</template>

<script>
export default {
  name: 'ScorllIntoView',
  data () {
    return {
      list1: [
        '热门推荐',
        '手机数码',
        '电脑办公',
        '家用电器',
        '计生情趣',
        '美妆护肤',
        '个护清洁',
        '汽车生活',
        '京东超市',
        '男装',
        '男鞋',
        '女装',
        '女鞋',
        '母婴童装',
        '图书音像',
        '运动户外',
        '内衣配饰',
        '食品生鲜',
        '酒水饮料',
        '家具家装',
        '家居厨具',
        '箱包手袋',
        '钟表珠宝',
        '玩具乐器'
      ],
      listCurrentIndex: 0,
      scrollTo: 0
    }
  },
  computed: {
    setStyle () {
      return `transform: translateY(-${this.scrollTo}px);`
    }
  },
  methods: {
    changeHandle (item, index) {
      this.listCurrentIndex = index
      this.scrollToCenter(index)
    },
    scrollToTop (index) {
      console.log(index)
      const itemHeight = 46 // 每个item高度
      const ulHeight = 1104 // ul 的高度
      const boxHeight = 375 // 盒子的高度
      if (ulHeight - boxHeight > (index - 1) * itemHeight) {
        this.scrollTo = index * itemHeight
      }
    },
    scrollToCenter (index) {
      console.log(index)
      const itemHeight = 46 // 每个item高度
      const ulHeight = 1104 // ul 的高度
      const boxHeight = 375 // 盒子的高度
      const currentItemPosition = itemHeight * index + itemHeight * 0.5 // 当前item 的中心位置
      if (
        currentItemPosition > boxHeight * 0.5 &&
        currentItemPosition + boxHeight * 0.5 < ulHeight + itemHeight
      ) {
        this.scrollTo =
          currentItemPosition - boxHeight * 0.5 - itemHeight * 0.5
      }
    }
  }
}
</script>

<style type="text/css">
html {
  color: #000;
  overflow-y: scroll;
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
  font-size: 76.8px;
}
html * {
  outline: 0;
  -webkit-text-size-adjust: none;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
body {
  background: #fff;
  color: #4a4f5a;
  font: 400 14px/1.5 "微软雅黑", Helvetica, STHeiti, Droid Sans Fallback;
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
body,
div,
dl,
dt,
dd,
ul,
ol,
li,
h1,
h2,
h3,
h4,
h5,
h6,
pre,
code,
form,
fieldset,
legend,
input,
textarea,
p,
blockquote,
th,
td,
hr,
button,
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  margin: 0;
  padding: 0;
}
input,
select,
textarea {
  font-size: 100%;
}
article,
aside,
footer,
header,
nav,
section,
audio,
canvas,
video,
img {
  display: block;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
fieldset,
img {
  border: 0;
}
abbr,
acronym {
  border: 0;
  font-variant: normal;
}
del {
  text-decoration: line-through;
}
address,
caption,
cite,
code,
dfn,
em,
th,
var {
  font-style: normal;
  font-weight: normal;
}
ol,
ul {
  list-style: none;
}
caption,
th {
  text-align: left;
}
h1,
h2,
h3,
h4,
h5,
h6 {
  font-size: 100%;
  font-weight: normal;
}
q:before,
q:after {
  content: "";
}
button {
  outline: 0;
  border: none;
}
a:hover {
  text-decoration: none;
}
ins,
a {
  text-decoration: none;
}
textarea {
  resize: none;
}
iframe,
img {
  border: 0;
}
img {
  display: block;
  width: 100%;
}
ul,
ol {
  list-style: none;
}
input,
select,
textarea {
  outline: 0;
  -webkit-user-modify: read-write-plaintext-only;
}
input {
  -webkit-appearance: none;
}
.pr {
  position: relative;
}
.abs {
  position: absolute;
}
.fl {
  float: left;
  display: inline-block;
}
.fr {
  float: right;
  display: inline-block;
}
.clearfix:after {
  content: ".";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}
.clearfix {
  *zoom: 1;
}
.wrap {
  min-width: 320px;
  max-width: 768px;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
}

.htit {
  line-height: 1rem;
  padding: 0 0.3rem;
  color: #ff8e2e;
  font-size: 0.4rem;
  margin: 1rem 0 0.4rem 0;
}
.wrapper01 {
  position: relative;
  height: 1rem;
  width: 1000px;
  overflow: hidden;
  margin: 0 auto;
  border-bottom: 1px solid #ccc;
}
.wrapper01 .scroller {
  position: absolute;
  display: flex;
  flex-wrap: nowrap;
}
.wrapper01 .scroller li {
  height: 1rem;
  color: #333;
  float: left;
  line-height: 1rem;
  font-size: 0.4rem;
  text-align: center;
}
.wrapper01 .scroller li a {
  color: #333;
  display: block;
  margin: 0 0.3rem;
}
.wrapper01 .scroller li.cur a {
  color: #1cbb9b;
}

.wrapper02 {
  position: relative;
  height: 1rem;
  width: 100%;
  overflow: hidden;
  margin: 0 auto;
  background: #ddd;
}
.wrapper02 .scroller {
  position: absolute;
}
.wrapper02 .scroller li {
  height: 1rem;
  color: #333;
  float: left;
  line-height: 1rem;
  font-size: 0.4rem;
  text-align: center;
}
.wrapper02 .scroller li a {
  color: #333;
  display: block;
  margin: 0 0.3rem;
}
.wrapper02 .scroller li.cur {
  background: #1cbb9b;
}
.wrapper02 .scroller li.cur a {
  color: #fff;
}

.wrapper03 {
  position: relative;
  height: 1rem;
  width: 100%;
  overflow: hidden;
  margin: 0 auto;
  border-bottom: 1px solid #ccc;
}
.wrapper03 .scroller {
  position: absolute;
}
.wrapper03 .scroller li {
  height: 1rem;
  color: #333;
  float: left;
  line-height: 1rem;
  font-size: 0.4rem;
  text-align: center;
}
.wrapper03 .scroller li a {
  color: #333;
  display: block;
  margin: 0 0.3rem;
  padding: 0 0.1rem;
}
.wrapper03 .scroller li.cur a {
  color: #1cbb9b;
  height: 0.9rem;
  border-bottom: 0.1rem solid #1cbb9b;
}

.wrapper04 {
  position: relative;
  height: 1rem;
  width: 80%;
  overflow: hidden;
  margin: 0 auto;
  background: #ddd;
}
.wrapper04 .scroller {
  position: absolute;
}
.wrapper04 .scroller li {
  height: 1rem;
  color: #333;
  float: left;
  line-height: 1rem;
  font-size: 0.4rem;
  text-align: center;
}
.wrapper04 .scroller li a {
  color: #333;
  display: block;
  margin: 0 0.3rem;
}
.wrapper04 .scroller li.cur {
  background: #1cbb9b;
}
.wrapper04 .scroller li.cur a {
  color: #fff;
}
</style>

<style lang="stylus" scoped>
.box
  margin 16px auto
  width 375px
  height 375px
  border 1px solid #cccccc
  overflow hidden
  ul
    width: 85px;
    transition all .2s ease 0s
  li
    height: 46px;
    line-height: 46px;
    background: #f8f8f8;
    text-align: center;
    position: relative;
    a
      color #333
    &.cur
      background: #fff;
      a
        color #e93b3d
</style>
